﻿using ModernShopping.AppCode;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

public static class CodeExtensions
{
    private const string MARKER = "415f48644024b2956d2d3a94a363a2bb";
    private const string LOGIN_INFO = "accef3561138151a8dc15a94e5ff5ad2";
    private const string SELECTED_STORE = "fdb0c388de01d545017cdf9ccf00eb72";
    private const string ADD_STORE = "2ae4cb57b05cb5410cef2c2d7fb7b53d";

    public static void SetMarker(this HttpSessionState Session, List<Marker> markers)
    {
        Session.Add(MARKER, markers);
    }

    public static void DownloadQrCode(this HttpResponse Response, string path, string fileName)
    {
        Response.Clear();
        Response.ContentType = "application/octet-stream";
        Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.png", fileName));
        Response.WriteFile(path);
        Response.End();
    }

    public static List<Marker> getMarker(this HttpSessionState Session)
    {
        try
        {
            List<Marker> markers = Session[MARKER] as List<Marker>;
            Session.Remove(MARKER);
            return markers;
        }
        catch
        {
            return null;
        }
    }

    public static void SetLoginInfo(this HttpSessionState Session, LoginInfo login)
    {
        Session.Add(LOGIN_INFO, login);
    }

    public static void SetLoginInfo(this HttpResponse Response, LoginInfo login)
    {
        HttpCookie MyCookie = new HttpCookie(LOGIN_INFO);
        MyCookie.Values.Add("UserID", login.UserID.ToString());
        MyCookie.Expires = DateTime.Now.AddMonths(1);
        Response.Cookies.Add(MyCookie);
    }

    public static LoginInfo GetLoginInfo(this HttpRequest request)
    {
        HttpCookie MyCookie = request.Cookies.Get(LOGIN_INFO);
        if (MyCookie != null)
        {
            string UserID = MyCookie.Values.Get("UserID");
            return new LoginInfo(Guid.Parse(UserID));
        }
        return null;
    }

    public static void RemoveLoginInfo(this HttpRequest Request, HttpResponse Response)
    {
        if (Request.Cookies[LOGIN_INFO] != null)
        {
            HttpCookie myCookie = new HttpCookie(LOGIN_INFO);
            myCookie.Expires = DateTime.Now.AddDays(-1d);
            Response.Cookies.Add(myCookie);
        }
    }

    public static void RemoveLoginInfo(this HttpSessionState Session)
    {
        Session.Remove(LOGIN_INFO);
    }
    public static LoginInfo GetLoginInfo(this HttpSessionState Session)
    {
        return Session[LOGIN_INFO] as LoginInfo;
    }

    public static void GoToHomePage(this HttpResponse Response, HttpRequest Request)
    {
        string URL = Request.Url.GetLeftPart(UriPartial.Authority) + "/Home.aspx";
        Response.Redirect(URL);
    }
    public static void GoToUserHomePage(this HttpResponse Response, HttpRequest Request)
    {
        string URL = Request.Url.GetLeftPart(UriPartial.Authority) + "/User/Home.aspx";
        Response.Redirect(URL);
    }

    public static void GoToLoginPage(this HttpResponse Response, HttpRequest Request)
    {
        string URL = Request.Url.GetLeftPart(UriPartial.Authority) + "/Account/Login.aspx";
        Response.Redirect(URL);
    }
    public static void GoToAddProductPage(this HttpResponse Response, HttpRequest Request)
    {
        string URL = Request.Url.GetLeftPart(UriPartial.Authority) + "/User/AddProducts.aspx";
        Response.Redirect(URL);
    }

    public static void GoToUpdateProductPage(this HttpResponse Response, HttpRequest Request)
    {
        string URL = Request.Url.GetLeftPart(UriPartial.Authority) + "/User/UpdateProduct.aspx";
        Response.Redirect(URL);
    }

    public static void GoToUpdateProductPage(this HttpResponse Response, HttpRequest Request, int ProductCode)
    {
        string URL = Request.Url.GetLeftPart(UriPartial.Authority) + "/User/UpdateProduct.aspx?ProductCode=" + ProductCode;
        Response.Redirect(URL);
    }

    public static void GoToQrCodeManagementPage(this HttpResponse Response, HttpRequest Request, int ProductCode)
    {
        string URL = Request.Url.GetLeftPart(UriPartial.Authority) + "/User/QrCodeManagement.aspx?ProductCode=" + ProductCode;
        Response.Redirect(URL);
    }

    public static void GoToProductInfoPage(this HttpResponse Response, HttpRequest Request, int ProductCode)
    {
        string URL = Request.Url.GetLeftPart(UriPartial.Authority) + "/ProductInfo.aspx?ProductCode=" + ProductCode;
        Response.Redirect(URL);
    }

    public static void GoToProductInStorePage(this HttpResponse Response, HttpRequest Request)
    {
        string URL = Request.Url.GetLeftPart(UriPartial.Authority) + "/User/ProductInStore.aspx";
        Response.Redirect(URL);
    }

    public static void GoToAccessDeniedPage(this HttpResponse Response, HttpRequest Request)
    {
        string URL = Request.Url.GetLeftPart(UriPartial.Authority) + "/AccessDenied.aspx";
        Response.Redirect(URL);
    }

    public static void GoToSearchPage(this HttpResponse Response, HttpRequest Request, string Keywords)
    {
        string URL = Request.Url.GetLeftPart(UriPartial.Authority) + "/Search.aspx?Keywords=" + Keywords;
        Response.Redirect(URL);
    }

    public static void SetSelectedStore(this HttpSessionState Session, Store store)
    {
        Session.Add(SELECTED_STORE, store);
    }

    public static Store GetSelectedStore(this HttpSessionState Session)
    {
        Store st = Session[SELECTED_STORE] as Store;
        Session.Remove(SELECTED_STORE);
        return st;
    }

    public static void AddStoreToList(this HttpSessionState Session, Store store)
    {
        List<Store> listStore = new List<Store>();
        if (Session[ADD_STORE] != null)
        {
            listStore = Session[ADD_STORE] as List<Store>;
        }
        listStore.Add(store);
        Session[ADD_STORE] = listStore;
    }

    public static void AddListStore(this HttpSessionState Session, List<Store> listStores)
    {
        if (Session[ADD_STORE] != null)
        {
            listStores = Session[ADD_STORE] as List<Store>;
        }
        Session[ADD_STORE] = listStores;
    }

    public static void RemoveListStore(this HttpSessionState Session)
    {
        if (Session[ADD_STORE] != null)
        {
            Session[ADD_STORE] = null;
        }
    }
    public static bool RemoveStore(this HttpSessionState Session, int Code)
    {
        List<Store> listStore = new List<Store>();
        if (Session[ADD_STORE] != null)
        {
            listStore = Session[ADD_STORE] as List<Store>;
            foreach (Store st in listStore)
            {
                if (st.Code.Equals(Code))
                {
                    listStore.Remove(st);
                    return true;
                }
            }
        }
        return false;
    }

    public static List<Store> GetListStore(this HttpSessionState Session)
    {
        return Session[ADD_STORE] as List<Store>;
    }

    public static void ClearListStore(this HttpSessionState Session)
    {
        Session.Remove(ADD_STORE);
    }
}